import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

// 导入element-ui库
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

// 导入全局样式
import "@/assets/css/base.css"

import "./api/mock"

import Cookie from "js-cookie";

Vue.use(ElementUI);

Vue.config.productionTip = false

// 添加全局前置守卫
router.beforeEach((to, from, next) => {
  // 判断token是否存在
  const token = Cookie.get("token")
  console.log(token);
  // token不存在，说明用户没有登录，应该跳转到登录页
  if (!token && to.name !== 'login') {
    next({ name: "login" })
  } else if (token && to.name === 'login') {//token存在，说明用户已经登录，此时跳转到首页
    next({ name: "home" })
  } else {
    next()
  }

})

new Vue({
  router,
  store,
  render: h => h(App),
  created() {
    store.commit("tab/addMenu", router)
  }
}).$mount('#app')
